package com.ruoyi.content.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.content.domain.ArticleTag;
import com.ruoyi.content.mapper.ArticleTagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.content.mapper.ArticleMapper;
import com.ruoyi.content.domain.Article;
import com.ruoyi.content.service.IArticleService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.file.FileUploadUtils;

/**
 * 文章 服务层实现
 * 
 * @author cloudoer
 * @date 2019-06-25
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
	@Autowired
	private ArticleMapper articleMapper;

	@Autowired
	private ArticleTagMapper articleTagMapper;

	/**
     * 查询文章信息
     * 
     * @param articleId 文章ID
     * @return 文章信息
     */
    @Override
	public Article selectArticleById(Long articleId)
	{
	    return articleMapper.selectArticleTagById(articleId);
	}
	
	/**
     * 查询文章列表
     * 
     * @param article 文章信息
     * @return 文章集合
     */
	@Override
	public List<Article> selectArticleList(Article article)
	{
	    return articleMapper.selectArticleTagList(article);
	}

	@Override
	public List<Article> selectArticleTagListRan(Article article) {
		return articleMapper.selectArticleTagListRan(article);
	}

	/**
     * 新增文章
     * 
     * @param article 文章信息
     * @return 结果
     */
	@Override
	public int insertArticle(Article article)
	{
	    return articleMapper.insertArticle(article);
	}
	
	/**
     * 修改文章
     * 
     * @param article 文章信息
     * @return 结果
     */
	@Override
	public int updateArticle(Article article, Long[] list)
	{
		articleTagMapper.deleteArticleTagById(article.getArticleId());
		for (Long l : list){
			articleTagMapper.insertArticleTag(new ArticleTag(article.getArticleId(), l));
		}
	    return articleMapper.updateArticle(article);
	}

	/**
     * 删除文章对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteArticleByIds(String ids)
	{
		File fileDirectory =new File(Global.getProfile() + "article/" + ids);
		if(fileDirectory.isDirectory()){
			fileDirectory.delete();
		}
		try {
			FileUtils.deleteDirectory(Global.getProfile() + "article/" + ids);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return articleMapper.deleteArticleByIds(Convert.toStrArray(ids));
	}

	@Override
	public String img(Long aid, MultipartFile file) throws IOException {
		String baseDir = Global.getProfile() + "article/" + aid + "/";
		long size = file.getSize();
		if (FileUploadUtils.DEFAULT_MAX_SIZE != -1 && size > FileUploadUtils.DEFAULT_MAX_SIZE){
			throw new FileSizeLimitExceededException(FileUploadUtils.DEFAULT_MAX_SIZE / 1024 / 1024);
		}
		//获取到后缀
		String prefix = FileUploadUtils.getExtension(file);
		String fileName = new Date().getTime() + "." + prefix;
		//判断文件夹是否存在，如不存在就创建文件夹
		File fileDirectory =new File(baseDir);
		if(!fileDirectory.isDirectory()){
			fileDirectory.mkdir();
		}
		file.transferTo(new File(baseDir + fileName));
		return Global.getFilePath() + "article/" + aid + "/" + fileName;
	}

}
